<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>收藏</title>
</head>
<body>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    let dishId = getParam(url, 'dishId');
    $(function () {
        $.ajax({
            url: "http://localhost:8080/collect.do",
            type: "post",
            data: {
                dishId: dishId,
                method: 'doCollect'
            },
            dataType: "text",
            success: function (data) {
                if (data === 'ok') {
                    alert('收藏成功');
                    window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
                }
                if (data === 'login') {
                    alert('请先登录');
                    window.location.href = 'DishPage.jsp?storeId=' + storeId;
                }
                if (data === "error") {
                    alert('收藏失败');
                    window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
                }
                if (data === 'exist'){
                    alert('您已经收藏该菜品！');
                    window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
                }
            }
        })
    })
</script>
</body>
</html>
